<template>
  <div id="addSeck_app">
    <el-dialog title="提示" :visible.sync="isShow.flag" width="50%">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="活动名称" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="一级分类" prop="first_cateid">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择活动区域"
            @change="changefirst"
          >
            <el-option
              :label="cate.catename"
              :value="cate.id"
              v-for="cate in cateList"
              :key="cate.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" prop="second_cateid">
          <el-select v-model="form.second_cateid" placeholder="请选择活动区域">
            <el-option
              :label="cate.catename"
              :value="cate.id"
              v-for="cate in cateList_children"
              :key="cate.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动商品" prop="giidsid">
          <el-select v-model="form.giidsid" placeholder="请选择活动区域">
            <el-option
              :label="cate.goodsname"
              :value="cate.id"
              v-for="cate in cateList_goods"
              :key="cate.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="begintime">
          <div class="block">
            <el-date-picker
              v-model="form.begintime"
              type="datetime"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="结束时间" prop="endtime">
          <div class="block">
            <el-date-picker
              v-model="form.endtime"
              type="datetime"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="活动状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="1">正常</el-radio>
            <el-radio label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isShow.flag = false">取 消</el-button>
        <el-button type="primary" @click="isShow.flag = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
export default {
  computed: {
    ...mapState("cate_model", ["cateList"]), //商品分类
    ...mapState("goods_model", ["goodsList"]), //商品管理
  },
  methods: {
    //改变一级分类获取点击的对应二级分类
    changefirst(id) {
      //所有数据再cateList
      var index = this.cateList.findIndex((item) => {
        return item.id === id;
      }); //查找所有的父级分类，id满足的返回当前分类的索引
      this.cateList_children = this.cateList[index].children;
    },

    ...mapActions("cate_model", ["cateListActions"]), //商品分类
    ...mapActions("goods_model", ["goodsListActions"]), //商品管理
  },
  created() {
    this.cateListActions();
    this.goodsListActions();
  },
  props: ["isShow"],
  data() {
    return {
      cateList_children: [],
      cateList_goods: [],
      form: {
        title: "",
        first_cateid: "",
        second_cateid: "",
        giidsid: "",
        begintime: "",
        endtime: "",
        status: "",
      },
      rules: {
        title: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
        first_cateid: [
          { required: true, message: "请选择一级分类", trigger: "blur" },
        ],
        second_cateid: [
          { required: true, message: "请选择二级分类", trigger: "blur" },
        ],
        giidsid: [
          { required: true, message: "请选择活动商品", trigger: "blur" },
        ],
        begintime: [
          { required: true, message: "请选择开始时间", trigger: "blur" },
        ],
        endtime: [
          { required: true, message: "请选择结束时间", trigger: "blur" },
        ],
        status: [
          { required: true, message: "请选择活动状态", trigger: "blur" },
        ],
      },
    };
  },
};
</script>

<style>
</style>